﻿@model ProductReviewHelpfulnessModel
@using Nop.Web.Models.Catalog;
<div class="product-review-helpfulness">
    @T("Reviews.Helpfulness.WasHelpful?")
    <div>
        <input type="button" id="vote-yes-@(Model.ProductReviewId)" class="vote" data-mini="true" data-inline="true" value="@T("Common.Yes")" />
        <input type="button" id="vote-no-@(Model.ProductReviewId)" class="vote" data-mini="true" data-inline="true" value="@T("Common.No")" />
        (<span id="helpfulness-vote-yes-@(Model.ProductReviewId)">@(Model.HelpfulYesTotal)</span>/<span id="helpfulness-vote-no-@(Model.ProductReviewId)">@(Model.HelpfulNoTotal)</span>)
        <div id="helpfulness-vote-result-@(Model.ProductReviewId)"></div>
    </div>
    
    <script type="text/javascript">
        $(document).ready(function () {
            $('#vote-yes-@(Model.ProductReviewId)').click(function () {
                setProductReviewHelpfulness@(Model.ProductReviewId)('true');
            });
            $('#vote-no-@(Model.ProductReviewId)').click(function () {
                setProductReviewHelpfulness@(Model.ProductReviewId)('false');
            });
        });

        function setProductReviewHelpfulness@(Model.ProductReviewId)(wasHelpful){
                //show progress
                $.mobile.showPageLoadingMsg();
                $.ajax({
                    cache: false,
                    type: "POST",
                    url: "@(Url.RouteUrl("SetProductReviewHelpfulness"))",
                    data: { "productReviewId": @(Model.ProductReviewId), "washelpful": wasHelpful },
                    success: function (data) {
                        //hide progress
                        $.mobile.hidePageLoadingMsg();
                         $("#helpfulness-vote-yes-@(Model.ProductReviewId)").html(data.TotalYes);
                         $("#helpfulness-vote-no-@(Model.ProductReviewId)").html(data.TotalNo);
                         $("#helpfulness-vote-result-@(Model.ProductReviewId)").html(data.Result);
                                                  
                         $('#helpfulness-vote-result-@(Model.ProductReviewId)').fadeIn("slow").delay(1000).fadeOut("slow");
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        //hide progress
                        $.mobile.hidePageLoadingMsg();

                        alert('Failed to vote. Please refresh the page and try one more time.');
                    }  
                });
        }
    </script>
</div>
